import tkinter as tk
from tkinter import messagebox, ttk
from datetime import datetime

class User:
    def __init__(self, username, password, user_type):
        self.username = username
        self.password = password
        self.user_type = user_type

class RepairRequest:
    def __init__(self, building, room_number, reason, urgency):
        self.building = building
        self.room_number = room_number
        self.reason = reason
        self.urgency = urgency
        self.timestamp = datetime.now()
        self.completed = False
        self.feedback = ""

class InspectionPlan:
    def __init__(self, name, date, building, floor):
        self.name = name
        self.date = date
        self.building = building
        self.floor = floor
        self.record = ""

class DormMaintenanceApp:
    def __init__(self, master):
        self.master = master
        self.master.title("湖工大学生宿管信息管理系统")
        self.master.geometry("600x500")
        self.master.configure(bg="#f0f0f0")

        self.users = [
            User("student1", "password1", "student"),
            User("dormmanager", "password2", "dorm_manager"),
            User("repairman", "password3", "repair_personnel"),
        ]

        self.repair_requests = [
            RepairRequest("西6", "203", "水管漏水", "紧急"),
            RepairRequest("北10", "108", "灯泡坏了", "不紧急"),
            RepairRequest("北11", "301", "空调不制冷", "紧急"),
            RepairRequest("西6", "121", "门锁坏了", "紧急")
        ]

        self.inspection_plans = []
        self.current_user = None
        self.create_login_screen()

    def create_login_screen(self):
        self.login_frame = tk.Frame(self.master, bg="#f0f0f0")
        self.login_frame.pack(pady=50)

        tk.Label(self.login_frame, text="湖工大学生宿管信息管理系统",
                 font=("Arial", 16, "bold"), bg="#f0f0f0").grid(row=0, column=0, columnspan=2, pady=(0, 20))

        tk.Label(self.login_frame, text="用户名:", bg="#f0f0f0").grid(row=1, column=0, sticky='w')
        self.username_entry = tk.Entry(self.login_frame, width=30)
        self.username_entry.grid(row=1, column=1)

        tk.Label(self.login_frame, text="密码:", bg="#f0f0f0").grid(row=2, column=0, sticky='w')
        self.password_entry = tk.Entry(self.login_frame, show="*", width=30)
        self.password_entry.grid(row=2, column=1)

        tk.Button(self.login_frame, text="登录", command=self.login,
                  bg="#4CAF50", fg="white", width=10).grid(row=3, columnspan=2, pady=20)

    def login(self):
        username = self.username_entry.get()
        password = self.password_entry.get()

        for user in self.users:
            if user.username == username and user.password == password:
                self.current_user = user
                self.login_frame.destroy()
                self.create_main_screen()
                return

        messagebox.showerror("登录失败", "用户名或密码错误")

    def create_main_screen(self):
        self.main_frame = tk.Frame(self.master)
        self.main_frame.pack(pady=10)

        if self.current_user.user_type == "dorm_manager":
            self.create_dorm_manager_interface()
        elif self.current_user.user_type == "repair_personnel":
            self.create_repair_personnel_interface()

    def create_dorm_manager_interface(self):
        # 左侧导航栏
        self.nav_frame = tk.Frame(self.main_frame, bg="#d3d3d3", width=150)
        self.nav_frame.pack(side=tk.LEFT, fill=tk.Y)

        tk.Label(self.nav_frame, text="宿管管理", bg="#d3d3d3").pack(pady=10)
        tk.Button(self.nav_frame, text="检查计划制定", command=self.create_inspection_plan, width=20).pack(pady=5)
        tk.Button(self.nav_frame, text="检查记录填写", command=self.fill_inspection_record, width=20).pack(pady=5)

        # 主内容区
        self.content_frame = tk.Frame(self.main_frame, bg="#f0f0f0")
        self.content_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)

    def create_inspection_plan(self):
        for widget in self.content_frame.winfo_children():
            widget.destroy()

        tk.Label(self.content_frame, text="检查计划制定", bg="#f0f0f0", font=("Arial", 14)).pack(pady=(10, 20))

        tk.Label(self.content_frame, text="计划名称:").pack()
        self.plan_name_entry = tk.Entry(self.content_frame, width=30)
        self.plan_name_entry.pack()

        tk.Label(self.content_frame, text="检查日期:").pack()
        self.check_date_entry = tk.Entry(self.content_frame, width=30)
        self.check_date_entry.pack()

        tk.Label(self.content_frame, text="检查楼栋:").pack()
        self.building_entry = tk.Entry(self.content_frame, width=30)
        self.building_entry.pack()

        tk.Label(self.content_frame, text="检查楼层:").pack()
        self.floor_entry = tk.Entry(self.content_frame, width=30)
        self.floor_entry.pack()

        tk.Button(self.content_frame, text="保存", command=self.save_inspection_plan).pack(pady=(20, 10))

    def save_inspection_plan(self):
        name = self.plan_name_entry.get()
        date = self.check_date_entry.get()
        building = self.building_entry.get()
        floor = self.floor_entry.get()

        if name and date and building and floor:
            new_plan = InspectionPlan(name, date, building, floor)
            self.inspection_plans.append(new_plan)
            messagebox.showinfo("保存成功", "检查计划已成功保存！")
            self.clear_entries()
        else:
            messagebox.showwarning("输入错误", "请填写所有信息！")

    def clear_entries(self):
        self.plan_name_entry.delete(0, tk.END)
        self.check_date_entry.delete(0, tk.END)
        self.building_entry.delete(0, tk.END)
        self.floor_entry.delete(0, tk.END)

    def fill_inspection_record(self):
        for widget in self.content_frame.winfo_children():
            widget.destroy()

        tk.Label(self.content_frame, text="检查记录填写", bg="#f0f0f0", font=("Arial", 14)).pack(pady=(10, 20))

        if not self.inspection_plans:
            tk.Label(self.content_frame, text="没有可填写的检查计划！", bg="#f0f0f0").pack()
            return

        tk.Label(self.content_frame, text="请选择检查计划:").pack()
        self.plan_var = tk.StringVar()
        self.plan_combobox = ttk.Combobox(self.content_frame, textvariable=self.plan_var)
        self.plan_combobox['values'] = [f"{plan.name} - {plan.building} {plan.floor}层 ({plan.date})" for plan in self.inspection_plans]
        self.plan_combobox.pack()

        tk.Label(self.content_frame, text="检查记录:").pack()
        self.record_entry = tk.Text(self.content_frame, width=50, height=10)
        self.record_entry.pack()

        tk.Button(self.content_frame, text="保存记录", command=self.save_inspection_record).pack(pady=(20, 10))

    def save_inspection_record(self):
        selected_index = self.plan_combobox.current()
        if selected_index == -1:
            messagebox.showwarning("未选择", "请选择一个检查计划！")
            return

        record = self.record_entry.get("1.0", tk.END).strip()
        if record:
            self.inspection_plans[selected_index].record = record
            messagebox.showinfo("保存成功", "检查记录已成功保存！")
            self.record_entry.delete("1.0", tk.END)  # 清空记录框
        else:
            messagebox.showwarning("输入错误", "请填写检查记录！")

    def create_repair_personnel_interface(self):
        # 左侧导航栏
        self.nav_frame = tk.Frame(self.main_frame, bg="#d3d3d3", width=150)
        self.nav_frame.pack(side=tk.LEFT, fill=tk.Y)

        tk.Label(self.nav_frame, text="维修人员管理", bg="#d3d3d3").pack(pady=10)
        tk.Button(self.nav_frame, text="待维修请求", command=self.show_repair_requests, width=20).pack(pady=5)

        # 主内容区
        self.content_frame = tk.Frame(self.main_frame, bg="#f0f0f0")
        self.content_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)

    def show_repair_requests(self):
        for widget in self.content_frame.winfo_children():
            widget.destroy()

        tk.Label(self.content_frame, text="待维修请求", bg="#f0f0f0", font=("Arial", 14)).pack(pady=(10, 20))

        if not self.repair_requests:
            tk.Label(self.content_frame, text="当前没有待维修请求", bg="#f0f0f0").pack()
            return

        for i, request in enumerate(self.repair_requests):
            frame = tk.Frame(self.content_frame, bg="#f0f0f0", relief="groove", borderwidth=1)
            frame.pack(pady=5, fill=tk.X)

            tk.Label(frame, text=f"请求编号: {i + 1}", bg="#f0f0f0").grid(row=0, column=0, sticky='w')
            tk.Label(frame, text=f"楼栋: {request.building}, 房间: {request.room_number}", bg="#f0f0f0").grid(row=1, column=0, sticky='w')
            tk.Label(frame, text=f"原因: {request.reason}", bg="#f0f0f0").grid(row=2, column=0, sticky='w')
            tk.Label(frame, text=f"紧急程度: {request.urgency}", bg="#f0f0f0").grid(row=3, column=0, sticky='w')

            if not request.completed:
                tk.Button(frame, text="标记为完成", command=lambda req=request: self.complete_repair_request(req)).grid(row=4, column=0, sticky="e", padx=5)
                feedback_entry = tk.Entry(frame, width=30)
                feedback_entry.grid(row=5, column=0, sticky='w', pady=(5, 0))
                feedback_entry.bind("<Return>", lambda event, req=request: self.add_feedback(req, feedback_entry.get()))

    def complete_repair_request(self, repair_request):
        repair_request.completed = True
        messagebox.showinfo("成功", "维修请求已标记为完成！")
        self.show_repair_requests()

    def add_feedback(self, repair_request, feedback):
        if feedback:
            repair_request.feedback = feedback
            messagebox.showinfo("反馈", "反馈已成功添加！")
        else:
            messagebox.showwarning("输入错误", "反馈不能为空！")

if __name__ == "__main__":
    root = tk.Tk()
    app = DormMaintenanceApp(root)
    root.mainloop()
